
package com.phx.serialjni;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
//import android.content.pm.*;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Message;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;


import com.utils.callMenu;

import java.util.List;

public class MainActivity extends Activity {

    myFrameLayout frame;
    int maxC = 0;
    public int currentPosition = -1;
    JBtnView newm;
    int currentPage = 0;
    PageJ pagestatus = new PageJ();
    Rect currentRect = new Rect();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wkmain);

        frame = (myFrameLayout) findViewById(R.id.gridlayout2);


        // PageS.setfeeds();


        refreshui();

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);




        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;

        //this.addContentView();

    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            //Toast.makeText(MainActivity.this,"BACK key").show();
//
//            callMenu.toast(MainActivity.this,"Back key call");
//            callMenu.closeAPk("com.kugou.android",MainActivity.this);
//           // callMenu.kill("com.kugou.android");
//        }
//        return  true;
//        //return super.onKeyDown(keyCode, event);
//    }


    public void removeAll()
    {
        if (frame != null) {
            frame.removeAllViews();
            frame.invalidate();
        }
        maxC = 0;
    }

    void refreshui()
    {


        removeAll();
        pagestatus.setInit(0);

        currentPage = PageJ.pageno;

        JBtnView mezi = new JBtnView(MainActivity.this);


        mezi.bitmapX = 100;
        mezi.bitmapY = 0;
        mezi.mleft = convertW(0);
        mezi.mtop = convertH(0);
        mezi.mwidth = convertW(10);
        mezi.mheight = convertH(10);
        mezi.mRes = R.drawable.b1_off;
        mezi.type = 1;
        mezi.setClickable(true);


        mezi.setId(0);
        // frame.setTag(allCmd.toString());
        mezi.setOnTouchListener(new MainActivity.TouchListenerx());

        frame.addView(mezi,-1);
        //  frame.setBackgroundResource(PageS.pageback);
        //   frame.setBackgroundResource(R.drawable.a6);
        // frame.addView(mezi,0);
        // frame.invalidate();

        addmezis();


    }


    public int convertW(int oldx) {
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;

        int newx = oldx * screenWidth / 1024;

        return newx;

    }

    public  Rect convertRect(Rect old)
    {
        return  new Rect(convertW(old.left),convertH(old.top),convertW(old.right),convertH(old.bottom));
    }
    public int convertH(int old) {
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;

        int newx = (old * screenHeight) / 600;

        return newx;

    }

    protected int rconvertW(int oldx) {
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;

        int newx = (oldx *  1024) / screenWidth;

        return newx;

    }

    protected int rconvertH(int old) {
        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;

        int newx = (600 * old) / screenHeight;

        return newx;

    }

    protected View createButton(int index) {
        final Button btn = new Button(this);
        btn.setId(index++);
        Log.d("da", "id is" + btn.getId());

        btn.setWidth(40);
        btn.setHeight(100);
        //GridLayout.LayoutParams mParams = new GridLayout.LayoutParams(
        //btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
        //       LinearLayout.LayoutParams.WRAP_CONTENT));
        // btn.set
        // btn.setLayoutParams(new GridLayout.LayoutParams(GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.MATCH_PARENT));
        btn.setText("okc");
        return btn;
    }


    public  void addmezis()
    {


        Rect tmprect = new Rect() ;
        Rect tmprecto = new Rect() ;
        int tmp =PageJ.allRect.length;
        maxC = tmp ;
        for (int w=0; w<tmp;w++) {
            // tmprecto = convertRect(PageS.allRect[w]);


            tmprecto = PageJ.allRect[w];
            JBtnView mezii = new JBtnView(MainActivity.this);

            // mezi.setOnTouchListener(new OnTouchListener() {
            //  @Override
            // public boolean onTouch(View view, MotionEvent event) {


            //  mezi.bitmapX = 100;
            //  mezi.bitmapY = 0;
            mezii.mleft = convertW(tmprecto.left);
            mezii.mtop = convertH(tmprecto.top);
            mezii.mwidth = convertW(tmprecto.width());
            mezii.mheight = convertH(tmprecto.height());

            mezii.mRes = PageJ.offArray[w];
            String addr = PageJ.feedDArray[w];
            //  boolean bool1 = PageJ.feedMap.containsKey(addr);
            String tmpS = PageJ.feedMap.get(addr);
            // boolean bool2 = (tmpS == null)? false:(tmpS.equals("0001"));


            mezii.type = PageJ.nodeArray[w];
            //   mezii.setClickable(true);

            // mezi.setTag(allCmd.toString());
            mezii.setId(w + 1);
            // frame.setTag(allCmd.toString());
            // mezii.setOnTouchListener(new TouchListenerx());

            //  frame.addView(mezi,0);
            //  frame.setBackgroundResource(pagestatus.pageback);
            // frame.addView(mezii);
            frame.addView(mezii,w);
            //   if(bool1 && bool2)
            // {
            //     mezii.mRes = PageJ.onArray[w];
            mezii.repaint();
            //testFeed(w,true);
            //  }
            //  testFeed(6);
            // frame.getChildAt()
        }
    }


    private void openApp(String packageName, String actName ,Context cx) {
//        PackageInfo pi = cx.getPackageManager().getPackageInfo(packageName, 0);
//
//        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
//        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//        resolveIntent.setPackage(pi.packageName);
//
//        List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
//
//        ResolveInfo ri = apps.iterator().next();
//        if (ri != null ) {
//            String packageName = ri.activityInfo.packageName;
//            String className = ri.activityInfo.name;
//
//            Intent intent = new Intent(Intent.ACTION_MAIN);
//            intent.addCategory(Intent.CATEGORY_LAUNCHER);
//
//            ComponentName cn = new ComponentName(packageName, className);
//
//            intent.setComponent(cn);
        // startActivity(intent);

        Intent intent=new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(new ComponentName(packageName, actName));
        cx.startActivity(intent);
    }



    private class TouchListenerx implements View.OnTouchListener {
        private boolean longClicked;
        private boolean upClicked;
        private int downtimes = 0;
        private int uptimes = 0;
        private int failTime;
        //private IBackService xBackService;
//        private TouchListenerx(IBackService abak) {
//         //   xBackService = abak;
//        }

        private TouchListenerx() {
            //   xBackService = abak;
        }


        int getCurSel(Rect[] allrect ,int x,int y)
        {

            //  if (tempRect.contains((int) x, (int) (y)))
            Rect tmprect = new Rect() ;
            Rect tmprecto = new Rect() ;
            int tmp = allrect.length;
            for (int w=0; w<tmp;w++)
            {
                tmprecto = convertRect(allrect[w]);
                if (tmprecto.contains(x,y))
                {
                    tmprect = tmprecto;
                    return  w;
                }
            }
            return  -1;
        }


        public boolean onTouch(View v, MotionEvent event) {

            //  iBackService.
            int x = (int) event.getX();
            int y = (int) event.getY();
            currentPosition = getCurSel(PageJ.allRect,x,y);

            Log.e("touchTAG", "view:"  + v.getId());


            if(currentPosition!=-1){
                String cmdOne = PageJ.cmdArray[currentPosition];
                String cmdTwo = PageJ.actArray[currentPosition];
                int mmtype = pagestatus.typeArray[currentPosition];


                // pageS.
                if (mmtype == 29)
                {
                    // pagestatus.cmdArray[1]
                    if (event.getAction() == 1) {
                        try {
                            openApp(cmdOne, cmdTwo, MainActivity.this);
                        }
                        catch (Exception e)
                        {
                            callMenu.toast(MainActivity.this,"请检查是否安装完整！");
                        }
                    }

                }
                else  if (mmtype == 28)
                {
                    if (event.getAction() == 1) {
                        Intent intent = new Intent(MainActivity.this, MainActivityw.class);
                        startActivity(intent);
                    }

                }



            }


            return  true;
        }
    }

}
